package com.delong.service;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.apache.ibatis.session.SqlSession;

import com.delong.dao.AppMapper;
import com.delong.dao.DaoHelper;
import com.delong.model.app.App;

public class AppService {

	public boolean addElement(App element) {
		SqlSession session = null;
		boolean result = false;
		try {
			session = DaoHelper.getSqlSessionFactory().openSession(true);
			AppMapper mapper = session.getMapper(AppMapper.class);
			int flag = mapper.addElement(element);
			result = flag > 0;
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			if (session != null) {
				session.close();
			}
		}
		return result;
	}

	public List<App> getListElement(int id, String title, int companyId, int categoryId, int source, int pageno,
			int limit, int status, String packageName,int searchParentCategory) {
		SqlSession session = null;
		List<App> result = new ArrayList<App>();
		try {
			session = DaoHelper.getSqlSessionFactory().openSession(true);
			AppMapper mapper = session.getMapper(AppMapper.class);
			result = mapper.selectList(id, title, companyId, categoryId, source, pageno, limit, status, packageName,searchParentCategory);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			if (session != null) {
				session.close();
			}
		}
		return result;

	}

	public int getListElementCount(int id, String title, int companyId, int categoryId, int source, int status,
			String packageName,int searchParentCategory) {
		SqlSession session = null;
		int result = 0;
		try {
			session = DaoHelper.getSqlSessionFactory().openSession(true);
			AppMapper mapper = session.getMapper(AppMapper.class);
			result = mapper.selectListcount(id, title, companyId, categoryId, source, status, packageName,searchParentCategory);
			// System.out.println(result);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			if (session != null) {
				session.close();
			}
		}
		return result;
	}

	public void changeStatus(int id, int status) {
		SqlSession session = null;

		try {
			session = DaoHelper.getSqlSessionFactory().openSession(true);
			AppMapper mapper = session.getMapper(AppMapper.class);
			mapper.changeStatus(id, status);

		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			if (session != null) {
				session.close();
			}
		}
	}

	public void changeElement(App element) {
		SqlSession session = null;

		try {
			session = DaoHelper.getSqlSessionFactory().openSession(true);
			AppMapper mapper = session.getMapper(AppMapper.class);
			mapper.changeElement(element);

		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			if (session != null) {
				session.close();
			}
		}

	}

	public App getElement(int id) {
		SqlSession session = null;
		App element = new App();
		try {
			session = DaoHelper.getSqlSessionFactory().openSession(true);
			AppMapper mapper = session.getMapper(AppMapper.class);
			element = mapper.selectElementById(id);

		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			if (session != null) {
				session.close();
			}
		}
		return element;

	}

	public App selectElementPackageName(String packageName,int id) {
		SqlSession session = null;
		App element = null;
		try {
			session = DaoHelper.getSqlSessionFactory().openSession(true);
			AppMapper mapper = session.getMapper(AppMapper.class);
			element = mapper.selectElementPackageName(packageName,id);
			
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			if (session != null) {
				session.close();
			}
		}
		return element;

	}

	public List<App> getListByCategoryId(int categoryId) {
		SqlSession session = null;
		List<App> result = new ArrayList<App>();
		try {
			session = DaoHelper.getSqlSessionFactory().openSession(true);
			AppMapper mapper = session.getMapper(AppMapper.class);
			result = mapper.selectListByCategoryId(categoryId);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			if (session != null) {
				session.close();
			}
		}
		return result;

	}

	public List<App> getListByCompanyId(int companyId) {
		SqlSession session = null;
		List<App> result = new ArrayList<App>();
		try {
			session = DaoHelper.getSqlSessionFactory().openSession(true);
			AppMapper mapper = session.getMapper(AppMapper.class);
			result = mapper.selectListByCompanyId(companyId);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			if (session != null) {
				session.close();
			}
		}
		return result;

	}

	public List<App> getListTagsId(int tagsId) {
		SqlSession session = null;
		List<App> result = new ArrayList<App>();
		try {
			session = DaoHelper.getSqlSessionFactory().openSession(true);
			AppMapper mapper = session.getMapper(AppMapper.class);
			result = mapper.selectListByTagsId(tagsId);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			if (session != null) {
				session.close();
			}
		}
		return result;

	}

}
